home *** CD-ROM | disk | FTP | other *** search
- /* FilterFirstOrderHighpass.c */
- /*****************************************************************************/
- /* */
- /* Out Of Phase: Digital Music Synthesis on General Purpose Computers */
- /* Copyright (C) 1994 Thomas R. Lawrence */
- /* */
- /* This program is free software; you can redistribute it and/or modify */
- /* it under the terms of the GNU General Public License as published by */
- /* the Free Software Foundation; either version 2 of the License, or */
- /* (at your option) any later version. */
- /* */
- /* This program is distributed in the hope that it will be useful, */
- /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
- /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
- /* GNU General Public License for more details. */
- /* */
- /* You should have received a copy of the GNU General Public License */
- /* along with this program; if not, write to the Free Software */
- /* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
- /* */
- /* Thomas R. Lawrence can be reached at tomlaw@world.std.com. */
- /* */
- /*****************************************************************************/
-
- /* Based on material from pages 184-190 of */
- /* Dodge, Charles and Jerse, Thomas A. */
- /* Computer Music: Synthesis, Composition, and Performance */
- /* Schirmer Books, New York, 1985 */
-
- #include "MiscInfo.h"
- #include "Audit.h"
- #include "Debug.h"
- #include "Definitions.h"
-
- #include "FilterFirstOrderHighpass.h"
- #include "Memory.h"
- #include "FloatingPoint.h"
-
-
- struct FirstOrderHighpassRec
- {
- /* link */
- FirstOrderHighpassRec* Next;
-
- /* state variables */
- float Ym1;
-
- /* coefficients */
- float A;
- float B;
- };
-
-
- static FirstOrderHighpassRec* FreeList = NIL;
-
-
- /* flush free list */
- void FlushCachedFirstOrderHighpassStuff(void)
- {
- while (FreeList != NIL)
- {
- FirstOrderHighpassRec* Temp;
-
- Temp = FreeList;
- FreeList = FreeList->Next;
- ReleasePtr((char*)Temp);
- }
- }
-
-
- /* create a new filter record */
- FirstOrderHighpassRec* NewFirstOrderHighpass(void)
- {
- FirstOrderHighpassRec* Filter;
-
- if (FreeList != NIL)
- {
- Filter = FreeList;
- FreeList = FreeList->Next;
- }
- else
- {
- Filter = (FirstOrderHighpassRec*)AllocPtrCanFail(sizeof(FirstOrderHighpassRec),
- "FirstOrderHighpassRec");
- if (Filter == NIL)
- {
- return NIL;
- }
- }
- Filter->Ym1 = 0;
- return Filter;
- }
-
-
- /* dispose filter record */
- void DisposeFirstOrderHighpass(FirstOrderHighpassRec* Filter)
- {
- CheckPtrExistence(Filter);
- Filter->Next = FreeList;
- FreeList = Filter;
- }
-
-
- /* adjust filter coefficients */
- void SetFirstOrderHighpassCoefficients(FirstOrderHighpassRec* Filter,
- float Cutoff, long SamplingRate)
- {
- float C;
-
- CheckPtrExistence(Filter);
- C = 2 - DCOS(6.28318530717958648 * Cutoff / SamplingRate);
- Filter->B = C - DSQRT(C * C - 1);
- Filter->A = 1 - Filter->B;
- }
-
-
- /* apply filter to a sample value */
- float ApplyFirstOrderHighpass(FirstOrderHighpassRec* Filter, float Xin)
- {
- CheckPtrExistence(Filter);
- Filter->Ym1 = Filter->A * Xin - Filter->B * Filter->Ym1;
- return Filter->Ym1;
- }
-